use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    // 构建邮件
    let email = Message::builder()
        .from("644284807@qq.com".parse()?)
        .to("644284807@qq.com".parse()?)
        .subject("Test Email from Rust")
        .body("This is a test email sent from Rust using lettre and QQ mail.".to_owned())?;

    // 设置 SMTP 服务器
    let creds = Credentials::new(
        "644284807@qq.com".to_string(),
        "gvqpotsfbxklbfee".to_string(),
    );
    let mailer = SmtpTransport::relay("smtp.qq.com")?
        .credentials(creds)
        .build();

    // 发送邮件
    match mailer.send(&email) {
        Ok(_) => println!("Email sent successfully!"),
        Err(e) => println!("Could not send email: {:?}", e),
    }

    Ok(())
}
